package com.mallcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.constant.CacheConstants;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.FloorModel;
import com.mallcloud.mall.product.service.FloorService;
import org.springframework.cache.CacheManager;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "楼层信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/floor")
public class FloorController {
    private final FloorService floorService;
    private final CacheManager cacheManager;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_floor_view')")
    public R<IPage<FloorModel>> page(Page page, FloorModel dto) {
        return R.ok(floorService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/listName")
    @PreAuthorize("@pms.hasPermission('product_floor_view')")
    public R<List<FloorModel>> listName(FloorModel dto) {
        return R.ok(floorService.list(Wrappers.lambdaQuery(dto)
                .select(FloorModel::getFloorId, FloorModel::getName)
                .eq(FloorModel::getStatus, true)
                .orderByAsc(FloorModel::getSortOrder)
        ));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_floor_view')")
    public R<List<FloorModel>> list(FloorModel dto) {
        return R.ok(floorService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_floor_view')")
    public R<FloorModel> getInfo(FloorModel dto) {
        return R.ok(floorService.getById(dto.getFloorId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_floor_add')")
    public R<Boolean> save(@RequestBody FloorModel dto) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(floorService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_floor_edit')")
    public R<Boolean> updateById(@RequestBody FloorModel dto) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(floorService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{floorId}")
    @PreAuthorize("@pms.hasPermission('product_floor_del')")
    public R<Boolean> removeById(@PathVariable Integer floorId) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(floorService.removeById(floorId));
    }

    @ApiOperation("批量删除楼层信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_floor_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> floorIds) {
        cacheManager.getCache(CacheConstants.PROJECT_BRAND_TREE).clear();
        return R.ok(floorService.removeByIds(floorIds));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_floor_export')")
    public List<FloorModel> export(FloorModel floor) {
        return floorService.list(Wrappers.query(floor));
    }
}
